package chapter21;

import java.util.HashSet;
import java.util.Set;

/**
 * @program: exercise
 * @description: 应用collection接口中方法
 * @author: 陈庆彪
 * @create: 2020-02-23 10:52
 **/
public class TestMethodsInCollection {
    public static void main(String[] args) {
        Set set1=new HashSet<>();
        set1.add("伦敦");
        set1.add("beijing");
        set1.add("shanghai");
        set1.add("guangzhou");
        set1.add("NewYork");
        set1.add("伦敦");
        System.out.println("set1 is"+set1);
        System.out.println(set1.size()+"elements in set1");

        //删除 set1 中的一个元素
        set1.remove("伦敦");
        System.out.println("set1 is"+set1);
        System.out.println(set1.size()+"elements in set1");


        Set set2=new HashSet<>();
        set2.add("北京");
        set2.add("武汉");
        set2.add("广州");
        set2.add("上海");
        System.out.println("\nset2 is"+set2);
        System.out.println(set2.size()+"elements in set2");
        System.out.println("\nis 北京 in set2?"+set2.contains("北京"));

        //将set2添加给set1
        set1.addAll(set2);
        System.out.println("\n"+set1);
        //从set1中删除set2
        set1.removeAll(set2);
        System.out.println("\n"+set1);
        //保留set1和set2中公共的元素
        set1.retainAll(set2);
        System.out.println("\n"+set1);





    }
}
